package cn.ghx.xboot.store;

import cn.ghx.xboot.attach.Attachment;
import cn.ghx.xboot.common.dto.IdNameDto;
import cn.ghx.xboot.item.ItemService;
import cn.ghx.xboot.setting.Setting;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.ghx.xboot.mapper.StoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author ghx
* @createDate 2024-03-08 10:45:13
*/
@Service
public class StoreService extends ServiceImpl<StoreMapper, Store>{

    private final ItemService itemService;

    @Lazy
    public StoreService(ItemService itemService) {
        this.itemService = itemService;
    }

    public Page<Store> query(String keyword,
                             String adcode,
                             Integer page, Integer size) {
        LambdaQueryChainWrapper<Store> qw = lambdaQuery();
        // 参数
        if (StringUtils.hasText(keyword)) {
            qw.and(w -> {
                w.like(Store::getName, keyword).or()
                        .like(Store::getAddress, keyword).or()
                        .like(Store::getRemark, keyword);
            });
        }
        if(StringUtils.hasText(adcode)){
            qw.eq(Store::getAdcode, adcode);
        }
        Page<Store> rs = qw.page(Page.of(page, size));
        rs.getRecords().forEach(this::wrapExtra);
        return rs;
    }

    private Store wrapExtra(Store item) {
        item.setMedias(getMedias(item.getId()));
        item.setTotalItem(itemService.countByStoreId(item.getId()));
        return item;
    }

    private List<Attachment> getMedias(String id) {
        return baseMapper.getMedias(id);
    }

    @CacheEvict(value = {
            "StoreService.getAllName"
    },allEntries = true)
    @Transactional
    public Boolean saveItem(Store item) {
        boolean rs = saveOrUpdate(item);
        List<Attachment> medias = item.getMedias();
        if(medias != null && !medias.isEmpty()){
            String storeId = item.getId();
            removeMedias(storeId);
            saveMedias(storeId,medias.stream().map(Attachment::getId).toList());
        }
        return rs;
    }

    private Boolean saveMedias(String storeId, List<String> attIds) {
        return baseMapper.saveMedias(storeId,attIds);
    }

    private Boolean removeMedias(String id) {
        return baseMapper.removeMedias(id);
    }

    @Cacheable("StoreService.getAllName")
    public List<IdNameDto> getAllName(String adcode) {
        return baseMapper.getAllName(adcode);
    }

    @CacheEvict(value = {
            "StoreService.getAllName"
    },allEntries = true)
    public Boolean deleteById(String id) {
        return removeById(id);
    }

    @Cacheable(value = "StoreService.findById", unless="#result == null")
    public Store findById(String id) {
        Store item = getById(id);
        return wrapExtra(item);
    }
}




